#include<bits/stdc++.h>
using namespace std;

struct patient{
	char number[15];
	int age;
}a[105];

bool cmp_oldf(patient x,patient y)
{
	if (x.age>=60||y.age>=60)
		return x.age>y.age;
	else return 0;
}//老年人在前

int main()
{
	int n;
	cin>>n;
	
	for (int i=0;i<n;i++)
		cin>>a[i].number>>a[i].age;
		
	stable_sort(a,a+n,cmp_oldf);
	
	for (int i=0;i<n;i++)
		cout<<a[i].number<<' '<<endl;
	
	return 0;
}